package com.imstlife.vending.utils;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.imstlife.vending.R;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.MySSLSocketFactory;
import com.loopj.android.http.RequestParams;

import org.apache.http.conn.ssl.SSLSocketFactory;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

public class HttpUtil {

	/**
	 * 判断当前网络是否可用
	 * 
	 * @param activity
	 *            当前Activity对象
	 * @return 可用返回true 否则返回false
	 * 
	 */
	public static boolean isNetworkAvailable(Activity activity) {
		
//		TimeUtils.reSetTime();
		Context context = activity.getApplicationContext();
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm == null) {
			return false;
		} else {
			NetworkInfo info[] = cm.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	/**
	 * 判断当前网络是否可用
	 * 
	 * @param context
	 * @return 可用返回true 否则返回false
	 * 
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm == null) {
			return false;
		} else {
			NetworkInfo info[] = cm.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}


//	private static AsyncHttpClient client = new AsyncHttpClient(
//			getSchemeRegistry()); // 实例化对象
	private static AsyncHttpClient client = new AsyncHttpClient(); // 实例化对象
	static {
		client.setTimeout(4000); // 设置链接超时，如果不设置，默认为3s
	}

	/**
	 * 用URL获取string对象
	 * 
	 * @param url
	 * @param res
	 */
	public static void get(String url, AsyncHttpResponseHandler res) {
		client.get(url, res);
	}

	/**
	 * 用toeken,URL获取string对象
	 * 
	 * @param tokenStr
	 * @param url
	 * @param res
	 */
	public static void get(String tokenStr, String url,
			AsyncHttpResponseHandler res) {
		client.addHeader("Authorization", "Bearer " + tokenStr);
		client.get(url, res);
	}

	/**
	 * 用URL,参数,获取string对象
	 * 
	 * @param url
	 * @param params
	 * @param res
	 */
	public static void get(String url, RequestParams params,
			AsyncHttpResponseHandler res) {
		client.get(url, params, res);
	}

	/**
	 * 用toeken,URL,参数,获取string对象
	 * 
	 * @param tokenStr
	 * @param url
	 * @param params
	 * @param res
	 */
	public static void get(String tokenStr, String url, RequestParams params,
			AsyncHttpResponseHandler res) {
		client.addHeader("Authorization", "Bearer " + tokenStr);
		client.get(url, params, res);
	}

	public static void get(Context context, String url, RequestParams params,
			AsyncHttpResponseHandler res) // url里面带参数
	{
		client.get(url, params, res);
	}

	public static void get(String url, JsonHttpResponseHandler res) // 不带参数，获取json对象或者数组
	{
		client.get(url, res);
	}

	public static void get(String url, RequestParams params,
			JsonHttpResponseHandler res) // 带参数，获取json对象或者数组
	{
		client.get(url, params, res);
	}

	public static void get(String url, BinaryHttpResponseHandler bHandler) // 下载数据使用，会返回byte数据
	{
		client.get(url, bHandler);
	}

	public static void get(String url, RequestParams params,
			BinaryHttpResponseHandler res) // 下载数据使用，会返回byte数据
	{
		client.get(url, params, res);
	}

	/**
	 * 用Post方式,获取string对象
	 * 
	 * @param tokenStr
	 * @param url
	 * @param res
	 */
	public static void post(String tokenStr, String url,
			AsyncHttpResponseHandler res) {
		client.addHeader("Authorization", "Bearer " + tokenStr);
	}

	/**
	 * 用Post方式,获取string对象
	 * 
	 * @param tokenStr
	 * @param url
	 * @param params
	 * @param res
	 */
	public static void post(String tokenStr, String url, RequestParams params,
			AsyncHttpResponseHandler res) {
		client.addHeader("Authorization", "Bearer " + tokenStr);
		client.post(url, params, res);
	}

	public static void postDoubanOauth(String tokenStr, String url,
			RequestParams params, AsyncHttpResponseHandler res) {
		client.addHeader("Authorization", "Bearer " + tokenStr);
		client.post(url, params, res);
	}

	/**
	 * 用Post方式,获取string对象
	 * 
	 * @param url
	 * @param params
	 * @param res
	 */
	public static void post(String url, RequestParams params,
			AsyncHttpResponseHandler res) {
		client.post(url, params, res);
	}

	public static AsyncHttpClient getClient() {
		return client;
	}
}